<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>用户列表</title>
<%@include file="/common/head.jsp" %>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>用户管理</span>
        </div>
        <div id="toolbar" style="float: right"></div>
    </div>
    <div class="page-title-border"></div>
</div>

<div class="search-field">
    <span class="field-cont">
        <span class="field-cont-title">登录名</span>
        <input type="text" class="summer-input" id="username"/>
    </span>
    
    <span class="field-cont">
        <span class="field-cont-title">姓名</span>
        <input type="text" class="summer-input" id="realname"/>
    </span>
    <span class="field-cont">
        <span class="field-cont-title">用户状态</span>
        <input type="text" class="summer-input" id="isUsable"/>
    </span>
    
    <span class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>

<div id="t-wrap">
    <table class="summer-datagrid" id="dataList">
	    <col width="15%">
	    <col width="15%">
	    <col width="15%">
	    <col width="21%">
	    <col width="12%">
	    <col width="22%">
	    <thead>
		    <tr>
		        <th>登录名</th>
				<th>姓名</th>
				<th>职务</th>
				<th>所属机构</th>
				<th>用户状态</th>
		        <th>操作</th>
		    </tr>
	    </thead>
    </table>
</div>

<script>
    //左侧组织机构树选中的机构码
    var org = '${param.org}',
        permission = CommonUtil.getPermission('user');
    
    $(function () {
        //初始化查询区用户状态
        $('#isUsable').combobox({
            valueField: 'code',
            textField: 'name',
            url: COMMON_URL.getCode + '?type=T_DM_USER_ISUSABLE'
        });
        
        // 新建toobar
		$.funcbar({
			id: 'toolbar',
			type: 'toolbar',
			data: permission,
			permit: ['user:initAdd']
		});
        
    	//初始化表格
    	createTable();
    	
        // 查询按钮
        $('#search').on('click', reloadTable);
        
        //重置按钮
        $('#reset').on('click', function () {
        	//重置文本框的值
        	$('#username').val('');
        	$('#realname').val('');
        	$('#isUsable').combobox('clear');
        	
        	//重置组织机构树
        	var pZtree = parent.ztree,
        	    nodes;
        	nodes = pZtree.getSelectedNodes();
        	if (nodes.length) {
        		pZtree.cancelSelectedNode(nodes[0]);
        		org = '';
        	}
        	reloadTable();
        });
        
        //新建
        $('#toolbar').on('click', 'a[data-permit="user:initAdd"]', function() {
        	parent.window.location.href = USER_CONST_URL.INIT_ADD + '?org='+org; 	  	
		});
        
        //编辑
        $('#t-wrap').on('click', 'a[data-permit="user:initEdit"]', function() {
            var $this = $(this),
                id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
            parent.window.location.href = USER_CONST_URL.INIT_EDIT + '?id=' + id;
        });	
        
        //删除
		$('#t-wrap').on('click', 'a[data-permit="user:delete"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			$.sdialog({
				type: 'confirm',
				msg: '您确定要删除选中的记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : USER_CONST_URL.DEL + "?ids=" + id,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '删除成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
        //查看
        $('#t-wrap').on('click', 'a[data-permit="user:initView"]', function() {
        	var $this = $(this),
        	    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
        	
        	parent.window.location.href = USER_CONST_URL.INIT_VIEW +"?id=" + id;
        });
        
        //角色分配
        $('#t-wrap').on('click', 'a[data-permit="user:initRoleAssign"]', function() {
        	 var $this = $(this),
        	     id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
        	
        	 parent.window.location.href = USER_CONST_URL.INIT_ROLE_ASSIGN + "?id=" + id;
        });
        
        //重置密码
        $('#t-wrap').on('click', 'a[data-permit="user:resetpwd"]', function() {
        	  var $this = $(this),
        	      id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
        	  $.sdialog({
  				type: 'confirm',
  				msg: '您确定要将密码重置为"123456"吗?',
  				onConfirm: function(result) {
  				    if (result) {
  				    	$.sajax({
  			                url : USER_CONST_URL.RESET_PWD + "?ids=" + id,
  			                onLoadSuccess : function() {
  			                	 $.stip({
                  	                type:'ok',//显示类型 有info、 ok、 danger
                  	                text: '密码重置成功！'    
  			                	});
  			                }
  			            });
  				    		
  				    }	
  				}
  			});  
        	  
        });
    });
    
    // 重新加载表格
	function reloadTable() {
		$('#dataList').datagrid('reload',{
            ajax: {
            	param: {
            		"realname": trimSpace($('#realname').val()),
                	"username": trimSpace($('#username').val()),
                	"isUsable": $('#isUsable').combobox('getValue'),
                	"org": org
            	}
            }
        });
	}
    
    //创建表格
    function createTable() {
    	$('#dataList').datagrid({
    		ajax: {
    			url: USER_CONST_URL.LIST,
    			param: {
            		"realname": trimSpace($('#realname').val()),
                	"username": trimSpace($('#username').val()),
                	"isUsable": $('#isUsable').combobox('getValue'),
                	"org": org
            	}
    		},
    		columns: [
    		     {
    		    	 field: 'id',
    		    	 hidden: true
    		     },
    		     {
    		    	 field: 'username',
    		    	 render: function (rowData) {
    		    		 return '<a href="javascript:;" data-permit="user:initView" class="link">'+ rowData.username +'</a>';
    		    	 }
    		     },
    		     {
    		    	 field: 'realname'
    		     },
    		     {
    		    	 field: 'position'
    		     },
    		     {
    		    	 field: 'org'
    		     },
    		     {
    		    	 field: 'isUsable'
    		     },
    		    {
    		    	 render: function (rowData) {
    		    		 return $.funcbar({
    		    			 data: permission,    //请求的数据
    		    			 type: 'text',     //类型
    		    			 display: 2,    //显示数量
    		    			 permit: ['user:initEdit', 'user:delete', 'user:initRoleAssign', 'user:resetpwd'] //需要的权限按钮
    		    		 });
    		    	 }
    		    }
    		          
    	    ]
    	});
    }
    
    //请求url
    var USER_CONST_URL = {
   		LIST: basePath + '/sys/user/list',
       	INIT_ADD: basePath + "/sys/user/initAdd",
       	INIT_EDIT: basePath + "/sys/user/initEdit",
       	DEL: basePath + "/sys/user/delete",
       	INIT_VIEW: basePath + "/sys/user/initView",
       	INIT_ROLE_ASSIGN: basePath + "/sys/user/initRoleAssign",       	
       	RESET_PWD: basePath + "/sys/user/resetPwd"
    };
    
</script>
</body>
</html>